home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / DFile.cpp < prev    next >
Encoding:
Text File  |  1996-07-05  |  13.9 KB  |  699 lines  |  [TEXT/R*ch]

  1. // DFile.cp
  2.  
  3.  
  4. #include <ncbi.h>
  5. #include <dgg.h>
  6. #include "DFile.h"
  7. #include "Dvibrant.h"
  8. #include "DId.h"
  9. #include <stdio.h>
  10.  
  11. #define NAMESTORE 
  12.  
  13. #if defined(COMP_CWI) || defined(OS_NT)
  14. #define WIN32 
  15. #undef FAR
  16. #undef NEAR
  17. #include <windows.h>
  18. #include <mapiwin.h>
  19.  
  20. #elif defined(OS_DOS)
  21. #include <dir.h>
  22. #include <dos.h>
  23. #endif
  24.  
  25. // class DFileManager
  26. // made into all static methods, Dec94
  27.  
  28. DFileManager*        gFileManager = NULL;
  29.  
  30. const char* DFileManager::kUntitled = "Untitled";
  31.  
  32. char    DFileManager::fName[640];
  33.  
  34.  
  35. void DFileManager::SetFilename( const char* name) 
  36. {
  37.     if (name) StrNCpy( fName, name, 512); else fName[0]= '\0';
  38. }
  39.  
  40. const char* DFileManager::GetInputFileName( const char* extension,  const char* mactype)
  41. {
  42.     Boolean okay= Nlm_GetInputFileName( (char*)fName, 512, (char*)extension, (char*)mactype);
  43.     if (okay) return fName;
  44.     else return NULL;
  45. }
  46.  
  47. const char* DFileManager::GetOutputFileName( const char* defaultname) 
  48. {
  49.     Boolean okay=  Nlm_GetOutputFileName( (char*)fName, 512, (char*)defaultname);
  50.     if (okay) return fName;
  51.     else return NULL;
  52. }
  53.  
  54. const char* DFileManager::GetFolderName()
  55. {
  56.     // this is a quick hack -- currently this selects a file w/in a folder,
  57.     // then chops filename off -- confusing to users, need folder select dialog
  58.     Boolean okay= Nlm_GetInputFileName( (char*)fName, 512, NULL, NULL);
  59.     if (okay) return DFileManager::PathOnlyFromPath(fName);
  60.     else return NULL;
  61. }
  62.     
  63. const char* DFileManager::GetProgramPath()
  64. {
  65.     Nlm_ProgramPath( (char*)fName, 512);
  66.     return fName;
  67. }
  68.  
  69. const char* DFileManager::FilenameFromPath( const char* pathname)
  70. {
  71.     return Nlm_FileNameFind( (char*)pathname);         
  72. }
  73.  
  74. const char* DFileManager::PathOnlyFromPath( const char* pathname)
  75. {
  76.     char* nameonly= Nlm_FileNameFind( (char*)pathname);
  77.     long len= StrLen(pathname) - StrLen(nameonly);
  78.     StrNCpy(fName, pathname, len);
  79.     fName[len]= 0;
  80.     return fName;
  81. }
  82.  
  83.  
  84.  
  85. short DFileManager::FileOrFolderExists( const char* pathname)
  86. {
  87.  
  88. #ifdef OS_MAC
  89.     long    theDirID;
  90.     Boolean isFolder = false;
  91.     char    namebuf[256];
  92.     
  93.     StrNCpy(namebuf, pathname, sizeof(namebuf));
  94.     Nlm_CtoPstr( namebuf);
  95.     short err= GetDirID( 0, 0, namebuf, &theDirID, &isFolder);
  96.     if (err == 0) {
  97.         if (isFolder) return kIsFolder;
  98.         else return kIsFile;
  99.         }
  100.     else return kNothing;
  101. #endif
  102.     
  103. #ifdef OS_UNIX
  104.     struct stat    statbuf;
  105.   if (! stat( pathname, &statbuf)) {
  106.         if ( S_ISDIR(statbuf.st_mode)) return kIsFolder;
  107.         else return kIsFile;
  108.         }
  109.     else return kNothing;
  110. #endif
  111.  
  112. #if defined(COMP_CWI) || defined(OS_NT)
  113.     WIN32_FIND_DATA finfo;
  114.     HANDLE hFindFile;
  115.     
  116.   MemFill( &finfo, 0, sizeof(finfo));
  117.   finfo.dwFileAttributes= 
  118.       FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_NORMAL;
  119.     hFindFile= FindFirstFile( (char*)pathname, &finfo);
  120.     if (hFindFile!=0) {
  121.         (void) FindClose(hFindFile);
  122.         if ( finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return kIsFolder;
  123.         else return kIsFile;
  124.         }
  125.     else return kNothing;
  126.     
  127. #elif defined(OS_DOS)
  128. #if defined(COMP_SYMC)
  129.   FIND* finfo= findfirst( pathname, FA_RDONLY | FA_DIREC);
  130.   if (finfo) {
  131.         if ( finfo->attribute & FA_DIREC) return kIsFolder;
  132.         else return kIsFile;
  133.     }
  134. #else
  135.     struct  ffblk  finfo;
  136.     
  137.   MemFill( &finfo, 0, sizeof(finfo));
  138.     short err= findfirst( pathname, &finfo, FA_RDONLY | FA_DIREC);
  139.     if (err==0) {
  140.         if ( finfo.ff_attrib & FA_DIREC) return kIsFolder;
  141.         else return kIsFile;
  142.         }
  143.     else return kNothing;
  144. #endif
  145. #endif
  146.  
  147. // for others...
  148.     return FileExists( pathname);
  149. }
  150.  
  151.  
  152. Boolean DFileManager::FileExists( const char* pathname)
  153. {
  154.     FILE* aFile= Nlm_FileOpen( (char*)pathname, "r");
  155.     if (aFile) {
  156.         Nlm_FileClose( aFile);
  157.         return true;
  158.         }
  159.     else
  160.         return false;
  161. }
  162.  
  163. Boolean DFileManager::Rename( const char* pathname, const char* newpathname)
  164. {
  165.         return Nlm_FileRename((char*)pathname, (char*)newpathname);
  166. }
  167.  
  168. Boolean DFileManager::IsRelativePath(const char* path)
  169. {
  170.     char* lpath= (char*) path;
  171.     
  172.     if (!lpath || !*lpath) return false;
  173.     
  174. #ifdef OS_MAC
  175.     return (*lpath == ':');
  176. #endif
  177.  
  178. #if defined(OS_DOS) || defined (OS_NT)
  179.     if (*lpath == '\\' || lpath[1] == ':') return false;
  180.     else return true;
  181. #endif
  182.  
  183. #ifdef OS_UNIX
  184.     return (*lpath != '/');
  185. #endif
  186.  
  187. #ifdef OS_VMS
  188.     // [some.path]file.name
  189.     // [-]file.name
  190.     // disk:[path.to.somewhere]file.name
  191.     if (StrChr(lpath,':') != 0) return false;
  192.     else {
  193.         char* cp= StrChr(lpath,'[');
  194.         if (!cp) return true;
  195.         else if (cp[1] == '.' || cp[1] == '-') return true;
  196.         else return false;
  197.         }
  198. #endif
  199.     return false;
  200. }
  201.  
  202.  
  203. char*    DFileManager::TempFolder( char* namestore)
  204. {
  205.     char* path;
  206.     if (!namestore) namestore= fName;
  207.     *namestore= 0;
  208.     
  209. #ifdef OS_MAC
  210.     path= Nlm_TmpNam(namestore); // uses FindFolder(kOnSystemDisk, kTemporaryFolderType ..
  211.     if (path) {
  212.         char* nameonly= Nlm_FileNameFind( path);
  213.         long len= StrLen(path) - StrLen(nameonly);
  214.         if (len > 512) len= 512;
  215.         StrNCpy( namestore, path, len);    
  216.         namestore[len]= 0;
  217.         }
  218. #endif
  219.  
  220. #if (defined(OS_UNIX) || defined(OS_DOS) || defined (OS_NT) || defined(OS_VMS))
  221.     path= getenv("TMPDIR");
  222.     if (!path) path= getenv("TMP");
  223.     if (!path) path= getenv("TEMP");
  224. #ifdef OS_UNIX
  225.     if (!path) path= "/tmp";
  226. #endif
  227.     if (path) StrNCpy( namestore, path, 512);
  228. #endif
  229.  
  230.     return namestore;
  231. }
  232.  
  233.  
  234. char*    DFileManager::TempFilename( char* namestore)
  235. {
  236.     return Nlm_TmpNam( namestore);
  237. }
  238.  
  239. char*    DFileManager::TempFilenameonly( char* namestore)
  240. {
  241.     char * nameonly, * path;
  242.     if (!namestore) namestore= fName;
  243.     *namestore= 0;
  244.     
  245.     path= Nlm_TmpNam( namestore);
  246.     if (path) {
  247.         nameonly= Nlm_FileNameFind( path);
  248.         if (nameonly) StrCpy( namestore, nameonly);    
  249.         return namestore;
  250.         }
  251.     else
  252.         return path;
  253. }
  254.         
  255. Boolean DFileManager::CreateFolder( const char* pathname)
  256. {
  257.     return Nlm_CreateDir( (char*) pathname);
  258. }
  259.  
  260.  
  261.  
  262. const char* DFileManager::BuildPath(const char* rootpath, const char* subfolder, const char* filename)
  263. {
  264.     if (rootpath) StrNCpy(fName, rootpath, 256);
  265.     else fName[0]= 0;
  266.     return Nlm_FileBuildPath(fName, (char*) subfolder, (char*) filename);
  267. }
  268.  
  269.  
  270. void DFileManager::UnixToLocalPath(char*& pathname)
  271. {
  272.     if (!pathname || !*pathname) return;
  273. #ifndef OS_UNIX
  274. #ifdef OS_VMS
  275.     // can't handle this yet .. can stdio calls under vms translate unix/style/paths?
  276. #else
  277. #ifdef OS_MAC
  278.     char buf[512], *np = buf, c;
  279.     char* cp= pathname;
  280.     long  n = 0;
  281.     
  282.     if (*cp == '/') cp++;         // absolute: start at 1st pathname
  283.     else { *np++= DIRDELIMCHR; n++; }     // relative: must insert DIRDEL before start !
  284.     do {
  285.         c= *cp++;
  286.         if (c == '/') c= DIRDELIMCHR;
  287.         *np++ = c; 
  288.         n++;
  289.     } while (c);
  290.     MemCpy(pathname, buf, n); // risky !!
  291.  
  292. #else
  293.     // MDOS, other?
  294.     char* cp= pathname;
  295.     do {
  296.         cp= StrChr( cp, '/');
  297.         if (cp) *cp++= DIRDELIMCHR; 
  298.     } while (cp);
  299. #endif
  300. #endif
  301. #endif
  302. }
  303.  
  304. const char* DFileManager::FileSuffix(const char* pathname)
  305. {
  306.   long     len = StrLen(pathname);
  307.   char* cp = (char*)pathname + len;
  308.   while (cp > pathname && *cp != DIRDELIMCHR) {
  309.         if (*cp == '.') return cp;
  310.         cp--;
  311.       }
  312.   return NULL;
  313. }
  314.  
  315.  
  316. void DFileManager::ReplaceSuffix(char* filename, long maxname, const char* suffix)
  317. {
  318.     long curlen, newlen;
  319.     char* psuf= (char*)FileSuffix(filename);
  320.     if (suffix) newlen= StrLen(suffix) + 1;
  321.   else newlen= 0;
  322.     if (psuf) {
  323.         curlen= StrLen(filename) - StrLen(psuf);
  324.         if (newlen > maxname - curlen) newlen= maxname - curlen;
  325.         if (suffix) StrNCpy( psuf, suffix, newlen);
  326.         else *psuf= 0;
  327.         }
  328.     else if (suffix) {
  329.         curlen= StrLen(filename);
  330.         if (newlen > maxname - curlen) newlen= maxname - curlen;
  331.         if (suffix) StrNCat( filename, suffix, newlen);
  332.         }
  333. }
  334.  
  335.  
  336.  
  337.  
  338.  
  339. //class DFile : public DObject
  340.  
  341.  
  342. char* DFile::fgType = NULL;
  343. char* DFile::fgSire = NULL;
  344.     
  345. DFile::DFile() 
  346. {
  347.     fFile= NULL;
  348.     fName= NULL;
  349.     fUseNameStore= false;
  350.     fMode= StrDup("r");
  351.     fType= fgType;
  352.     fSire= fgSire;
  353.     fEof= true;
  354. }
  355.  
  356.     
  357. DFile::DFile(const char* filename, const char* openmode,  
  358.                         const char* type, const char* creator)
  359. {
  360.     fFile= NULL;
  361.     fName= NULL;
  362.     fUseNameStore= false;
  363.     fMode= NULL;
  364.     fType= fgType;
  365.     fSire= fgSire;
  366.     fEof= true;
  367.     Initialize( filename, openmode, type, creator);
  368. }
  369.  
  370. void DFile::Initialize(const char* filename, const char* openmode,
  371.                         const char* type, const char* creator)
  372. {
  373.     char * name, * mode;
  374.     
  375.     if (fFile) Close(); 
  376. #ifdef NAMESTORE
  377.     Boolean freeName= (!fUseNameStore);
  378.     if (!filename) { 
  379.         fUseNameStore= true; 
  380.         *fNameStore= 0;
  381.         name= fNameStore; 
  382.         }
  383.     else {
  384.         fUseNameStore= (StrLen((char*) filename) < sizeof(fNameStore));
  385.         if (fUseNameStore) {
  386.             StrCpy(fNameStore, (char*) filename);
  387.             name= fNameStore;
  388.             }
  389.         else {
  390.             name= StrDup( (char*) filename); // do before free in case these are some of self's data
  391.             }
  392.         }
  393.     if (freeName) MemFree(fName);
  394.     fName= name;
  395. #else
  396.     name= StrDup( (char*) filename); // do before free in case these are some of self's data
  397.     MemFree(fName);
  398.     fName= name;
  399. #endif
  400.  
  401.     mode= StrDup( (char*) openmode);
  402.     MemFree(fMode);
  403.     fMode= mode;
  404.     
  405.     fType= (char*)type;
  406.     fSire= (char*)creator;
  407.     if (!fType) fType= fgType;
  408.     if (!fSire) fSire= fgSire;
  409.     fEof= true;
  410. }
  411.     
  412. DFile::~DFile() 
  413. {
  414.     if (fFile) Close();
  415. #ifdef NAMESTORE
  416.     if (!fUseNameStore) 
  417. #endif
  418.         MemFree(fName);
  419.     MemFree( fMode);
  420. }
  421.  
  422. Boolean DFile::suicide(void) 
  423.     if (GetOwnerCount() <= 1) { 
  424.         delete this; 
  425.         return true; 
  426.         }
  427.     else 
  428.         return DObject::suicide();
  429. }
  430.  
  431. Boolean DFile::suicide(short ownercount)
  432. {
  433.     if (ownercount < 1) {
  434.         delete this; 
  435.         return true; 
  436.         }
  437.     else 
  438.         return DObject::suicide(ownercount);
  439. }
  440.  
  441. Boolean DFile::Exists()
  442. {
  443.     if (fFile) return true; // this is only around if file exists
  444.     else if (!fName) return false;
  445.     else {
  446.         short kind= DFileManager::FileOrFolderExists(fName);
  447.         if (kind == DFileManager::kIsFile) return true;
  448.         else if (kind == DFileManager::kIsFolder) return false; //?? what to do?? false or true?
  449.         else return DFileManager::FileExists(fName); // try another test?
  450.         }
  451. }
  452.  
  453. void DFile::Create(const char* filetype, const char* creator)
  454. {
  455.     // this doesn't delete existing file...! on MacOS
  456.     fType= (char*)filetype;
  457.     fSire= (char*)creator;
  458. #ifndef OS_MAC
  459.  if (! DFileManager::FileExists(fName))
  460. #endif
  461.     Nlm_FileCreate(fName, (char*)filetype, (char*)creator);
  462. }
  463.  
  464. Boolean DFile::Delete()
  465. {
  466.     Close();
  467.     return Nlm_FileRemove(fName);
  468. }
  469.  
  470.  
  471. void DFile::SetName(const char* newname)
  472. {
  473.     if (newname && *newname && StrCmp(newname, fName)!=0) {
  474.         Close();
  475. #ifdef NAMESTORE
  476.         if (!fUseNameStore) MemFree(fName);
  477.         fUseNameStore= (StrLen((char*) newname) < sizeof(fNameStore));
  478.         if (fUseNameStore) {
  479.             StrCpy(fNameStore, (char*) newname);
  480.             fName= fNameStore;
  481.             }
  482.         else {
  483.             fName= StrDup( (char*) newname); 
  484.             }
  485. #else
  486.         MemFree(fName);
  487.         fName= StrDup( (char*) newname); 
  488. #endif
  489.     }
  490. }
  491.  
  492.  
  493. Boolean DFile::Rename(const char* newname)
  494. {
  495.     Boolean okay= false;
  496.     if (newname && *newname && StrCmp(newname, fName)!=0) {
  497.         okay= Nlm_FileRename(fName, (char*)newname);
  498.         if (okay) SetName( newname);
  499.         }
  500.     return okay;
  501. }
  502.  
  503.  
  504. void DFile::SetMode(const char* openmode)
  505. {
  506.     if (openmode && StringCmp(openmode, fMode) != 0) {
  507.         Nlm_FileClose( fFile);
  508.         char* mode= StrDup( (char*) openmode);
  509.         MemFree(fMode);
  510.         fMode= mode;
  511.         }
  512. }
  513.  
  514.         // result in all funcs here is error code or 0
  515. short DFile::Open(const char* openmode)
  516. {
  517.     if (openmode) {
  518.         if (StringCmp(openmode, fMode) == 0) 
  519.             openmode= NULL;
  520.         else {
  521.             char* mode= StrDup( (char*) openmode);
  522.             if (fMode) MemFree(fMode);
  523.             fMode= mode;
  524.             }
  525.         }
  526.     if (fFile) 
  527.         if (openmode) {
  528.             Nlm_FileClose( fFile);
  529.             fFile= Nlm_FileOpen(fName, fMode);
  530.             }
  531.         else fseek(fFile, 0, 0); // reset
  532.     else {
  533.         if ((fSire || fType) && (*fMode == 'w' || *fMode == 'a'))
  534.             Create( fType, fSire);
  535.         fFile= Nlm_FileOpen(fName, fMode);
  536.         }
  537.     fEof= (fFile == NULL);
  538.     return (fFile) ? 0 : -1;
  539. }
  540.  
  541.  
  542. short DFile::Close()
  543. {
  544.     if (fFile) Nlm_FileClose( fFile);
  545.     fEof= true;
  546.     fFile= NULL;
  547.     return 0;
  548. }
  549.  
  550. short DFile::GetDataLength(ulong& filelen)
  551. {
  552.     filelen= Nlm_FileLength(fName);
  553.     return 0; // any error code is trapped in Nlm_
  554. }
  555.  
  556. short DFile::GetDataMark(ulong& fileindex)
  557. {
  558.     // not yet available in ncbi toolkit...
  559.     short err= 0;
  560.     fileindex= 0;
  561.     if (!fFile) return -1;
  562.     long findex= ftell(fFile);
  563.     if (findex<0) {
  564.         err= findex;
  565.         fileindex= 0;
  566.         }
  567.     else
  568.         fileindex= findex;
  569.     return err;
  570. }
  571.  
  572. short DFile::SetDataMark(ulong    fileindex)
  573. {
  574.             // not yet available in ncbi toolkit...
  575.     if (!fFile) return -1;
  576.     else return fseek(fFile, fileindex, 0);
  577. }
  578.  
  579.  
  580. Boolean DFile::EndOfFile()
  581. {
  582.     if (!fFile) return -1;
  583.     else {
  584.         if (!fEof) fEof= feof(fFile); // !! need this !! this isn't always RIGHT !!
  585.         return fEof;
  586.         }
  587. }
  588.  
  589. short DFile::GetFileType(long& fileType) 
  590. {
  591.         // this is a silly hack -- needs work?
  592.     if (fMode && strchr(fMode,'t')) fileType= cTEXT;
  593.     else fileType= 0;
  594.     return 0;
  595. }
  596.  
  597. short DFile::ReadData( void* buffer, ulong& count) 
  598. {
  599.     long newcount= Nlm_FileRead(buffer, 1, count, fFile);
  600.     if (newcount == count) return 0;
  601.     else { 
  602.         if (newcount==0) fEof= true;
  603.         count= newcount; 
  604.         return -1; 
  605.         } 
  606. }
  607.  
  608. short DFile::ReadLine( char* line, ulong count) 
  609. {
  610.     void *buf= Nlm_FileGets(line, count, fFile);
  611.     if (buf) return 0;
  612.     else {
  613.         fEof= true;
  614.         return -1;
  615.         }
  616. }
  617.  
  618.  
  619. short DFile::ReadUntil( void* buffer, ulong& count, char stopchar) 
  620. {
  621.                 // no ncbi proc for this...
  622.     register int    c;
  623.     register char* bp = (char*) buffer;
  624.     long maxcount= count;
  625.  
  626.     if (!fFile || !buffer) return -1;
  627.     count= 0;
  628.     while (true) {
  629.         c = fgetc( fFile);
  630.         if (c<0) {
  631.             //if (c == EOF) 
  632.             fEof= true;
  633.             return c;
  634.             }
  635.         else {
  636.             *bp++= c;
  637.             count++;
  638.             if (c == stopchar || count >= maxcount)  
  639.                 return 0; // ?? do we stuff stopchar into buffer?
  640.             }
  641.         }
  642.     return 0;
  643. }
  644.  
  645.  
  646. short DFile::WriteLine( char* line, Boolean addNewline) 
  647. {
  648.     short result;
  649.     result= Nlm_FilePuts( line, fFile);
  650.     if (addNewline) result += Nlm_FilePuts( LineEnd, fFile);
  651.     return result;
  652. }
  653.  
  654. short DFile::WriteData( void* buffer, ulong& count)
  655. {
  656.     ulong newcount= Nlm_FileWrite(buffer, 1, count, fFile);
  657.     if (count != newcount) { count= newcount; return -1; } //??
  658.     else return 0;
  659. }
  660.  
  661.  
  662.  
  663. // class DTempFile
  664.  
  665. DTempFile::DTempFile() : 
  666.     DFile()
  667. {
  668.     // ?? text/binary mode flag
  669.     char    namestore[512];
  670.     Initialize( gFileManager->TempFilename(namestore), "a", NULL, NULL);
  671.     Open();
  672. }
  673.     
  674. DTempFile::~DTempFile()
  675. {
  676.     Delete();
  677. }    
  678.  
  679. char* DTempFile::ReadIntoMemory(ulong& bytesread, Boolean deleteAfterRead)
  680. {
  681.     short     err;
  682.     ulong    count;
  683.     char    * data;
  684.     
  685.     Open("r");
  686.     err= GetDataLength( count);
  687.     data= (char*) MemNew( count+1);
  688.     if (data) {
  689.         err= ReadData( data, count);
  690.         bytesread= count;
  691.         data[count]= 0; // add null term
  692.         }
  693.     Close();
  694.     if (deleteAfterRead) Delete();
  695.     return data;
  696. }
  697.  
  698.